|
|
 |
|
|
|
|
Dx = px - x1 - Wx * B
Dy = py - y1 - Wy * B
Dz = pz - z1 - Wz * B |
|
|
|
|
|
|
|
|
Then subtracting Radius2 from both sides of the equation gives: |
|
|
|
 |
|
|
|
|
0 = (t * Cx + Dx)2 + (t * Cy + Dy)2 + (t * Cz + Dz)2 - Radius2 = t2 * (Cx2 + Cy2 + Cz2) +
t * 2 * (Cx * Dx + Cy * Dy + Cz * Dz) +
(Dx2 + Dy2 + Dz2 - Radius2) |
|
|
|
|
|
|
|
|
You can simplify this again by making one final set of substitutions. |
|
|
|
 |
|
|
|
|
t2 * A1 + t * B1 + C1 = 0 |
|
|
|
 |
|
|
|
|
A1 = Cx2 + Cy2 + Cz2
B1 = 2 * (Cx * Dx + Cy * Dy + Cz * Dz)
C1 = Dx2 + Dy2 + Dz2 - Radius) |
|
|
|
|
|
|
|
|
Finally, you can use the quadratic formula to solve this equation for t. |
|
|
|
 |
|
|
|
|
t = (-B1 ± Sqr(B12 - 4 * A1 * C1)) / 2 / A1 |
|
|
|
|
|
|
|
|
When you enter the values for A1, B1, and C1, you get one of three possible results. First, if B12 4 * A1 * C1 < 0, the square root has no real solutions. In this case, the ray does not intersect the cylinder in real, three-dimensional space. |
|
|
|
|
|
|
|
|
Second, if B12 4 * A1 * C1 = 0, the equation has a single solution t = -B1/2/A1. In this case the ray is tangent to the cylinder, meeting it at a single point. |
|
|
|
|
|
|
|
|
Finally, if B12 4 * A1 * C1 > 0, the equation gives two possible values for t. Values less than zero represent points that lie behind the point at which the ray originates. These intersections are not interesting to a ray-tracing program. Larger values for t represent points further from the beginning of the ray than the points represented by smaller values. Thus, the position on the cylinder closest to the point (px, py, pz) in the direction of the vector <Vx, Vy, Vz> corresponds to the smaller positive value of t. |
|
|
|
|
|
|
|
|
Once you have determined t, you can use that value in the parametric equations for the ray to determine H, the actual point of intersection. |
|
|
|
|
|
|
|
|
You can also use the value of t and the equation u = A * t + B to solve for the corresponding value of u. Then, using this value and the parametric equations for the cylinder's axis, you can find the point I. Finally, from points H and I you can find the vector R. This vector is normal to the cylinder's surface at the point H. You can use it as the surface normal when you calculate the color of the pixel in the HitColor subroutine. |
|
|
|
|
|
|
|
|
In many programs, you will want to use a cylinder of finite length. In that case, you do not want to display intersections that occur beyond the ends of the cylinder. To detect |
|
|
|
|
|